<?php

namespace App\Admin\Controllers\Stat;

use App\Http\Controllers\Controller;
use App\Model\Gift;
use App\Model\Stat\StatDailySysGift;
use Encore\Admin\Controllers\ModelForm;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;

class StatDailySysgiftController extends Controller
{
    use ModelForm;

    public function index()
    {
        $content = new Content();
        $content->header('名义礼物日报');
        $content->body($this->grid());
        return $content;
    }

    /**
        $table->integer('send_num')->default(0)->comment('推送系统礼物总数');
        $table->integer('receive_num')->default(0)->comment('领取系统礼物总数');
        $table->integer('send_from_num')->default(0)->comment('推送礼物男用户数');
        $table->integer('send_to_num')->default(0)->comment('收到礼物女用户数');
        $table->integer('receive_user_num')->default(0)->comment('领取系统礼物用户数');
        $table->integer('receive_gift_coin')->default(0)->comment('被领取礼物总价值');
     */
    protected function grid()
    {
        $grid = new Grid(new StatDailySysGift());
        $grid->model()->orderBy('dt', 'desc')->orderBy('gift_id', 'asc');
        $grid->dt('日期');
        $grid->gift_id('礼物类型')->display(function ($data) {
            if (!$this->gift_id) return '全部礼物';
            $giftInfo = Gift::find($this->gift_id);
            return !empty($giftInfo->name) ? $giftInfo->name . "  ( 金币: {$giftInfo->coin} )" : '';
        });
        $grid->send_num('推送礼物总数');
        $grid->receive_num('被接收礼物总数');
        $grid->receive_percent('礼物接收率')->display(function ($data) {
            if (!$this->send_num) return '-';
            return number_format($this->receive_num / $this->send_num, 2) * 100 . "%";
        });
        $grid->send_from_num('推送礼物男用户数');
        $grid->send_to_num('推送礼物女女用户数');
        $grid->receive_user_num('接收礼物女女用户数');
        $grid->receive_host_percent('礼物女女用户接收率')->display(function ($data) {
            if (!$this->send_to_num) return '-';
            return number_format($this->receive_user_num / $this->send_to_num, 2) * 100 . "%";
        });
        $grid->receive_gift_coin('接收礼物总价值');
        $grid->actual_income_coin('实际礼物到账金币')->display(function ($data) {
            return $this->gift_id ? '-' : $data;
        });
        $grid->disableExport();
        $grid->disableRowSelector();
        $grid->disableActions();
        $grid->filter(function ($filter) {
            $filter->expand();
            $filter->disableIdFilter();
            $filter->column(1 / 3, function ($filter) {
                $filter->date('dt', '日期');
            });
            $filter->column(1 / 3, function ($filter) {
                $giftList = Gift::getList(2);
                $giftList[0] = '全部礼物';
                $filter->equal('gift_id', '礼物')->select($giftList);
            });
        });
        $grid->actions(function (Grid\Displayers\Actions $actions) {
            $actions->disableDelete();
            $actions->disableEdit();
            $actions->disableView();
        });
        return $grid;
    }

    function getChannels()
    {
        $list = AppChannel::where(['flag' => 1])->select(['channel_name', 'channel'])->get()->toArray();
        $data = [];
        foreach ($list as $item) {
            $data[] = ['id' => $item['channel'], 'text' => $item['channel_name']];
        }
        return $data;
    }
}
